package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class AddressBook here.
 * 
 * @author Rasmus Andersen Borup 
 * @version 
 */
public class AddressBook
{
    // instance variables
    private ArrayList<Person> persons;
    private static AddressBook instance;

    /**
     * Constructor for objects of class AddressBook
     */
    private AddressBook()
    {
        // initialising instance variables
        persons = new ArrayList<Person>();
    }
    
    public static AddressBook getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }
        
    public Person getPerson(String phone)
    {
        boolean found = false;
        int i = 0;
        if(persons.size() > 0)
        {
            while(!found && i <= persons.size())
            {
                if(persons.get(i).getPhone().equals(phone))
                {
                    found = true;
                }
                else
                {
                    i++;
                }
            }
        }
        return persons.get(i);
    }
    
    public ArrayList<Person> getPersons()
    {
        return persons;
    }

    public void addPerson(Person person)
    {
        persons.add(person);
    }
    
    public void removePerson(String phone)
    {
        persons.remove(getPerson(phone));
    }
    
    public void printInfo(String phone)
    {
        getPerson(phone).printInfo();
    }
    
    public void printPersons()
    {
        for(Person person : persons)
        {
            person.printInfo();
            System.out.println();
        }
    }
}
